package cn.example.common.protocol.modules;

import org.artifact.core.lang.IServer;
import org.artifact.core.context.session.ISession;
import org.artifact.core.context.packet.IPacket;
import cn.hutool.core.map.MapProxy;
import java.util.function.Consumer;
import java.util.Collection;
import java.util.Map;
import java.util.List;
import org.artifact.core.constant.ArtifactErrorCode;
import org.artifact.core.exception.ArtifactExceptionUtil;
import org.artifact.core.lang.IModule;
import cn.example.common.protocol.structs.*;

@SuppressWarnings("unchecked")
public interface PropModule extends IModule{
	
	default int getModuleId(){
		return 9059;
	}
	
	/** 分发  */
	default Consumer<IPacket> disp(ISession session,IPacket packet) {
		switch (packet.getMethodId()) {
			case 16778:{
				return useProp(session,packet);
			}
			default:
				throw ArtifactExceptionUtil.error(ArtifactErrorCode.NOT_FOND_ACTION);
		}
	}	


	/** 使用物品 */
	default Consumer<IPacket> useProp(ISession session,IPacket packet) {
		MapProxy proxy = MapProxy.create(packet.getBody());
		int num = proxy.getInt(109446);
		long propId = proxy.getLong(-979804194);
		return useProp(session, num, propId);
	}
	
	/** 使用物品 */
	Consumer<IPacket> useProp(ISession session, int num, long propId);
	
	default Consumer<IPacket> _useProp(ISession session, java.util.Map<java.lang.Integer, java.lang.Integer> reward){
		return (IPacket packet) -> {
                if (packet == null){
                    packet = IServer.me().getContext().getPacketFactory().create();
                    packet.setCmd(9059 << 16 ^ 16778);
                } else {
                    packet.getBody().clear();
                }

				packet.getBody().put(-934326481,reward);
				session.sendPacket(packet);
			};
	}
	
	
	/** 推送物品 */
	default void notifyProp(ISession session, java.util.Map<java.lang.Long, java.lang.Integer> props){
		IPacket packet = IServer.me().getContext().getPacketFactory().create();
		packet.setCmd(9059 << 16 ^ 25164);
		packet.getBody().put(106940784,props);
		session.sendPacket(packet);
	}
	
	default void notifyProp(Collection<ISession> sessions, java.util.Map<java.lang.Long, java.lang.Integer> props){
		IPacket packet = IServer.me().getContext().getPacketFactory().create();
		packet.setCmd(9059 << 16 ^ 25164);
		packet.getBody().put(106940784,props);
		for (ISession session : sessions) {
			session.sendPacket(packet);
		}
	}
	
}